<?php
require_once 'lib/controller.php';
require_once 'db/AlertsCheckDB.php';
require_once '../../models/EmailModel.php';
require_once 'views/AlertsCheckView.php';
require_once 'models/AlertsCheckModel.php';
$alertsCheckEmailModel = new EmailModel();
$alertsCheckDB = new AlertsCheckDB($db);
$alertsCheckView = new AlertsCheckView($html);
$alertsCheckModel = new AlertsCheckModel($alertsCheckDB, $alertsCheckView, $alertsCheckEmailModel, $helper);
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
function charityExists($charityName) {
    global $db;
    $sql = <<< EOT
        SELECT ID
        FROM Charity
        WHERE Name = '%s'

EOT;
    $sql = sprintf($sql, $db->escape($charityName));
    $result = $db->query($sql);
    $id = 0;
    if ($db->rowCount($result) > 0) {
        $row = $db->fetch($result);
        $value = $db->unescape($row['ID']);
        $id = intval($value);
    }
    if(empty($id)) {
        $id = insertCharity($charityName);
    }
    return $id;
}
function insertCharity($charityName) {
    global $db;
    $sql = <<< EOT
        INSERT INTO Charity(Name)
            VALUES('%s')

EOT;
    $sql = sprintf($sql, $db->escape($charityName));
    $result = $db->query($sql);
    return $db->insertID();
}
// :TODO: Needs to accept a person class instead
function updatePerson($id, $surname, $firstnames, $formername, $neename, 
        $alsoknownas, $deathdate, $birthdate, $age, $agemeasure, 
        $cityid, $formercityid, $regionid, $countryid, 
        $charityid, $funeralhomeid, $otherinfo)
{
    global $db;
    if (empty($id)) {
        return 0;
    }
    $sql = <<< EOT
        UPDATE Person
            SET Surname = '%s',
                FirstNames = '%s',
                FormerName = '%s',
                NeeName = '%s',
                AlsoKnownAs = '%s',
                DeathDate = '%s',
                BirthDate = '%s',
                Age = '%s',
                AgeMeasure = '%s',
                CityTownID = %d,
                FormerCityTownID = %d,
                RegionID = %d,
                CountryID = %d,
                CharityID = %d,
                FuneralHomeID = %d,
                OtherInfo = '%s',
                AddedOn = '%s'
        WHERE ID = %d

EOT;
     $sql = sprintf($sql,
         $db->escape($surname),
         $db->escape($firstnames), 
         $db->escape($formername), 
         $db->escape($neename), 
         $db->escape($alsoknownas), 
         $db->escape($deathdate), 
         $db->escape($birthdate), 
         $db->escape($age), 
         $db->escape($agemeasure), 
         $db->escape($cityid), 
         $db->escape($formercityid),
         $db->escape($regionid),
         $db->escape($countryid),
         $db->escape($charityid),
         $db->escape($funeralhomeid),
         $db->escape($otherinfo), 
         $db->escape(gmdate("Y-m-j H:i:s", time() + 3600*12)),
         $db->escape($id));
    $result = $db->query($sql);
    return intval($id);
}

$personId = intval($helper->post("ID"));
$lastName = $helper->post("surname");
$firstName = $helper->post("firstname");
$formerName = $helper->post("formername");
$neeName = $helper->post("neename");
$alsoKnownAs = $helper->post("alsoknownas");
$deathDate = $helper->post("deathdate");
$birthDate = $helper->post("birthdate");
$age = $helper->post("age");
$ageMeasure = $helper->post("agemeasure");
$cityTown = $helper->post("citytown");
$formerCityTown = $helper->post("formercitytown");
$region = $helper->post("region");
$country = $helper->post("country");
$charity = $helper->post('charity');
$funeralHomeId = intval($helper->post("funeralhome"));
$otherNotes = $helper->post("othernotes");
if(!empty($lastName)) {
    $charityId = charityExists($charity);
    $personId = updatePerson($personId, $lastName, $firstName, $formerName, $neeName, $alsoKnownAs, $deathDate, $birthDate, $age, $ageMeasure, $cityTown, $formerCityTown, $region, $country, $charityId, $funeralHomeId, $otherNotes);
    echo $personId;
    $alertsCheckModel->check($personId);
}
?>
